package common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Command implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6221571684516031649L;

	public enum CommandType
	{
		Get,
		Set
	}
	
	///command parameter
	private List<Param> commandParameter = null;
	
	private CommandType type;
	
	public List<Param> CommonParameter()
	{
		return commandParameter;
	}
	
	public CommandType getType()
	{
		return type;
	}
	
	public Command(CommandType cmdtype, Param ...data)
	{
		this.type = cmdtype;
		int i = data.length ;
		commandParameter = new ArrayList<>();
		for(int j = 0; j < i; j++)
			commandParameter.add(data[j]);
	}
	
	public byte[] toBytes() throws IOException
	{
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(output);
		out.writeObject(this);
		return output.toByteArray();
	}
	
	public static Command ParseFromBytes(byte[] data) throws ClassNotFoundException, IOException
	{
		Object o  =  new ObjectInputStream(new ByteArrayInputStream(data)).readObject();
		return (Command)o;
	}
	
}
